package com.xiaoran.client.connector.handler;

import com.xiaoran.common.data.RpcRequest;
import com.xiaoran.common.utils.JsonSerializerUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class RpcClientEncodeHandler extends MessageToByteEncoder<RpcRequest> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, 
                          RpcRequest rpcRequest, ByteBuf in) throws Exception {
        byte[] serialize = JsonSerializerUtil.serialize(rpcRequest);
        int length = serialize.length;
        in.writeInt(length);
        in.writeBytes(serialize);
    }
}
